<?xml version="1.0" encoding="utf-8" ?>
<h3>login page</h3>

<div style="margin-top: 20px" >
    <div><input id="name" type="text" name="username"></div>
    <div><input id="password" type="password" name="password"></div>
    <div><button type="button" onclick="submit()">登 录</button></div>
</div>

<script>
    function submit() {
        let data = {'username':document.querySelector("#name").value,'password': document.querySelector("#password").value}
        let href=location.href

        let pageUrl=getParameter(href,"pageUrl")
        let showPage=getParameter(href,"showPage")

        let req=new XMLHttpRequest()
        req.open("post","http://"+location.hostname+":"+location.port+"/login?pageUrl="+pageUrl+"&showPage="+showPage)
        req.setRequestHeader("Content-Type","application/json")
        req.onload=()=>{
            if(req.status==200){
                let arr=JSON.parse(req.responseText).val.split(";")
                location.href=arr[1]+"?token="+arr[0]+"&showPage="+arr[2]
            }
        }
        req.send(JSON.stringify(data))
    }

    function getParameter(href,expire) {
        let tar=expire+"="
        let idx=href.indexOf(expire)+tar.length

        if(idx==-1)
            return null

        let to=href.indexOf("&",idx)
        to=(to==-1?href.length:to)
        return href.substr(idx,to-idx)
    }
</script>